package netdb.courses.softwarestudio.smail.mvc.view;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import netdb.courses.softwarestudio.smail.mvc.ModelAwareServlet;
import netdb.courses.softwarestudio.smail.mvc.model.domain.Mail;
import netdb.courses.softwarestudio.smail.service.json.JsonService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


@SuppressWarnings("serial")
public class MailJsonView extends ModelAwareServlet<Mail> {
	private static final Log log = LogFactory.getLog(MailJsonView.class);
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		if(log.isDebugEnabled()) {
			log.debug("Responsing 200 OK");
		}
		Object m = getModel(req); // m can be a Mail object or a list of Mail objects
		resp.setContentType("application/json");
		resp.setHeader("Cache-Control", "no-cache"); // make sure no intermediate node caches the result
		resp.setCharacterEncoding("UTF-8");
		resp.getWriter().print(JsonService.serialize(m));
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if(log.isDebugEnabled()) {
			log.debug("Responsing 201 Created");
		}
		Mail m = (Mail) getModel(req);
		resp.setStatus(201);
		/* 
		 * Append id to the request URI of the controller rather than that of the current 
		 * view.
		 * The URI information of a forwarding servlet can be accessed by the request attributes
		 * javax.servlet.forward.[request_uri|context_path|servlet_path|path_info|query_string] in
		 * the forwared servlet. 
		 */
		resp.setHeader("Location", req.getAttribute("javax.servlet.forward.request_uri")
				+ "/" + m.getId());
	}
	
	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if(log.isDebugEnabled()) {
			log.debug("Responsing 204 No Content");
		}
		resp.setStatus(204);
	}

}
